package com.education.user.controller;

import com.github.pagehelper.PageHelper;
import com.education.common.utils.ApiReturnObject;
import com.education.user.pojo.SysEdition;
import com.education.user.service.SysEditionService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * APP版本 信息操作处理
 *
 * @author d
 * @date 2020-05-28
 */
@RestController
@RequestMapping("/sysEdition")
public class SysEditionController {


    @Autowired
    private SysEditionService sysEditionService;


    /**
     * 查询APP版本列表
     */
    @GetMapping("/list")
    public ApiReturnObject list(SysEdition sysEdition, Integer pageNum, Integer pageSize) {
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<SysEdition> list = sysEditionService.selectSysEditionList(sysEdition);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (SysEdition edition : list) {
            edition.setCreateTimeString(format.format(edition.getCreateTime()));
        }
        return new ApiReturnObject("200", "查询成功", new PageInfo<>(list));
    }

    /**
     * 查询最新记录
     */
    @GetMapping("/newList")
    public ApiReturnObject newList(Integer endpoint) {
        List<SysEdition> list = sysEditionService.selectSysEditionNewList(endpoint);
        return new ApiReturnObject("200", "查询成功", list.get(0));
    }


    /**
     * 新增APP版本
     */
    @PostMapping("/add")
    public ApiReturnObject add(@RequestBody SysEdition edition) throws ParseException {
        if (edition.getCreateTimeString() != null && !"".equals(edition.getCreateTimeString())) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date date = simpleDateFormat.parse(edition.getCreateTimeString());
            edition.setCreateTime(date);
            edition.setCode(edition.getPackageUrl());
            sysEditionService.insertSysEdition(edition);
            return new ApiReturnObject("200", "添加成功", "");
        }
        return new ApiReturnObject("201", "缺少参数", "");
    }


    /**
     * APP版本详情
     */
    @GetMapping("/selectOne")
    public ApiReturnObject selectOne(Integer id) {
        SysEdition sysEdition = sysEditionService.selectSysEditionById(id);
        return new ApiReturnObject("200", "查询成功", sysEdition);
    }


    /**
     * 修改保存APP版本
     */
    @PostMapping("/update")
    public ApiReturnObject editSave(@RequestBody SysEdition sysEdition) {
        if (sysEdition.getId() != null) {
            sysEditionService.updateSysEdition(sysEdition);
            return new ApiReturnObject("200", "修改成功", "");
        }
        return new ApiReturnObject("201", "缺少id", "");
    }

    /**
     * 删除APP版本
     */
    @PostMapping("/del")
    public ApiReturnObject remove(@RequestBody SysEdition edition) {
        if (edition.getId() != null) {
            sysEditionService.deleteSysEditionById(edition.getId());
            return new ApiReturnObject("200", "删除成功", "");
        }
        return new ApiReturnObject("201", "缺少id", "");
    }
}